﻿using System.Drawing;

namespace DimensionsHelper.ExcelAddIn.Core.Sql;

public class CodeMapSettingsTransmission : IExcelAddInTransmission
{
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 开始读取的行号，最小值是1，如果设置的值小于1，视为1.
    /// </summary>
    public int StartRow { get; set; } = 1;

    /// <summary>
    /// 码号列号，1开始，查找内容时，将从前向后依次查找，找到第一个非空值为止
    /// </summary>
    public int[] CodeColumns { get; set; } = [];

    /// <summary>
    /// 描述列号，1开始，查找内容时，将从前向后依次查找，找到第一个非空值为止
    /// </summary>
    public int[] LabelColumns { get; set; } = [];

    /// <summary>
    /// 分类码号级别的区分方式
    /// </summary>
    public string LevelMarkType { get; set; } = CodeLevelMarkType.InteriorColor.ToString().ToUpper();

    /// <summary>
    /// 分类码号级别的组织方式
    /// </summary>
    public string OrderType { get; set; } = CodeLevelOrderType.InteriorColor.ToString().ToUpper();

    /// <summary>
    /// 分类码号的颜色级别定义
    /// </summary>
    public int[][] ColorLevels { get; set; } = [];


    /// <summary>
    /// 针对码号分类级别做的额外处理方式，此属性只影响输出结果，不影响码表读取。
    /// </summary>
    public string LevelBehavior { get; set; } = CodeLevelBehavior.None.ToString().ToUpper();


    public IExcelAddInSettings ToSettings()
    {
        CodeMapSettings settings = new()
        {
            Name = Name,
            StartRow = StartRow,
            CodeColumns = CodeColumns,
            LabelColumns = LabelColumns,
        };

        if (Enum.TryParse<CodeLevelMarkType>(LevelMarkType, true, out var markType))
        {
            settings.LevelMarkType = markType;
        }

        if (Enum.TryParse<CodeLevelOrderType>(OrderType, true, out var orderType))
        {
            settings.OrderType = orderType;
        }

        if (Enum.TryParse<CodeLevelBehavior>(LevelBehavior, true, out var behavior))
        {
            settings.LevelBehavior = behavior;
        }
        
        settings.ColorLevels = ColorLevels;

        return settings;
    }

    public void ToSettings(IExcelAddInSettings settings)
    {
        if (settings is not CodeMapSettings codeMap)
        {
            return;
        }

        codeMap.Name = Name;
        codeMap.StartRow = StartRow;
        codeMap.CodeColumns = CodeColumns;
        codeMap.LabelColumns = LabelColumns;
        codeMap.ColorLevels = ColorLevels;

        if (Enum.TryParse<CodeLevelMarkType>(LevelMarkType, true, out var markType))
        {
            codeMap.LevelMarkType = markType;
        }

        if (Enum.TryParse<CodeLevelOrderType>(OrderType, true, out var orderType))
        {
            codeMap.OrderType = orderType;
        }

        if (Enum.TryParse<CodeLevelBehavior>(LevelBehavior, true, out var behavior))
        {
            codeMap.LevelBehavior = behavior;
        }
    }
}
